'''
Created on 2011-4-21

@author: Narcissu
'''
import win32api, win32pdh, win32pdhutil
import time
 
def ShowAllProcesses():
 
    procs = []
    object = win32pdhutil.find_pdh_counter_localized_name("Process")
 
    items, instances = win32pdh.EnumObjectItems(None,None,object,win32pdh.PERF_DETAIL_WIZARD)
    instance_dict = {}
    print instances
    for instance in instances:
        try:
#            if instance == 'svchost':
            instance_dict[instance] = instance_dict[instance] + 1
        except KeyError:
            instance_dict[instance] = 0
 
    items = [win32pdhutil.find_pdh_counter_localized_name("ID Process")] + items[0:]
 
    for instance, max_instances in instance_dict.items():
 
        for inum in xrange(max_instances+1):
 
            hq = win32pdh.OpenQuery()
 
            hcs = []
            for item in items:
                path = win32pdh.MakeCounterPath((None,object, instance,None, inum, item))
                hcs.append(win32pdh.AddCounter(hq, path))
 
            win32pdh.CollectQueryData(hq)
 
            time.sleep(0.01)
            win32pdh.CollectQueryData(hq)
            proc = instance[:15]
            hc = hcs[0]
 
            vals = []
            for i in range(len(hcs)):
                hc = hcs[i]
                type, val = win32pdh.GetFormattedCounterValue(hc, win32pdh.PDH_FMT_LONG)
                vals.append(val)
 
            win32pdh.RemoveCounter(hc)
 
            procs.append([proc, [vals] ])
            win32pdh.CloseQuery(hq)
 
#    print procs
    return procs
list = ShowAllProcesses()
for item in list:
    print item